﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;
using System.Data;

namespace ElingOA6_DSJCJ.DAL
{
    public partial class Calendar
    {
        public Calendar()
        { }
        #region  BasicMethod
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(decimal FCalendarID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from dbo.t_Calendar");
            strSql.Append(" where FCalendarID=@FCalendarID");
            SqlParameter[] parameters = {
					new SqlParameter("@FCalendarID", SqlDbType.Decimal)
			};
            parameters[0].Value = FCalendarID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public decimal Add(ElingOA6_DSJCJ.Model.Calendar model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into dbo.t_Calendar(");
            strSql.Append("FTitle,FContents,FIsWake,FBeginDate,FEndDate,FPri,FLastWakeTime,FNextWakeTime,FIsAllDayEvent,FWakeMinutes,FEntityNo,FEmplNo,FInputTime)");
            strSql.Append(" values (");
            strSql.Append("@FTitle,@FContents,@FIsWake,@FBeginDate,@FEndDate,@FPri,@FLastWakeTime,@FNextWakeTime,@FIsAllDayEvent,@FWakeMinutes,@FEntityNo,@FEmplNo,@FInputTime)");
            //  strSql.Append(";select @@IDENTITY");
            strSql.Append(";select @returnBId=IDENT_CURRENT('dbo.t_Calendar')");
            SqlParameter[] parameters = {
					new SqlParameter("@FTitle", SqlDbType.NVarChar,255),
					new SqlParameter("@FContents", SqlDbType.Text),
					new SqlParameter("@FIsWake", SqlDbType.Char,1),
					new SqlParameter("@FBeginDate", SqlDbType.DateTime),
					new SqlParameter("@FEndDate", SqlDbType.DateTime),
					new SqlParameter("@FPri", SqlDbType.Char,1),
					new SqlParameter("@FLastWakeTime", SqlDbType.DateTime),
					new SqlParameter("@FNextWakeTime", SqlDbType.DateTime),
					new SqlParameter("@FIsAllDayEvent", SqlDbType.Char,1),
					new SqlParameter("@FWakeMinutes", SqlDbType.Int,4),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FEmplNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FInputTime", SqlDbType.DateTime),
                                        new SqlParameter("@returnBId", SqlDbType.Decimal)};
            parameters[0].Value = model.FTitle;
            parameters[1].Value = model.FContents;
            parameters[2].Value = model.FIsWake;
            parameters[3].Value = model.FBeginDate;
            parameters[4].Value = model.FEndDate;
            parameters[5].Value = model.FPri;
            parameters[6].Value = model.FLastWakeTime;
            parameters[7].Value = model.FNextWakeTime;
            parameters[8].Value = model.FIsAllDayEvent;
            parameters[9].Value = model.FWakeMinutes;
            parameters[10].Value = model.FEntityNo;
            parameters[11].Value = model.FEmplNo;
            parameters[12].Value = model.FInputTime;
            parameters[13].Direction = ParameterDirection.Output;
            //  object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            //if (obj == null)
            //{
            //    return 0;
            //}
            //else
            //{
            //    return Convert.ToDecimal(obj);
            //}
            if (rows > 0)
            {
                model.FCalendarID = decimal.Parse(parameters[13].Value.ToString());
                return decimal.Parse(parameters[13].Value.ToString()); ;
            }
            else
            {
                return 0;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(ElingOA6_DSJCJ.Model.Calendar model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update dbo.t_Calendar set ");
            strSql.Append("FTitle=@FTitle,");
            strSql.Append("FContents=@FContents,");
            strSql.Append("FIsWake=@FIsWake,");
            strSql.Append("FBeginDate=@FBeginDate,");
            strSql.Append("FEndDate=@FEndDate,");
            strSql.Append("FPri=@FPri,");
            strSql.Append("FLastWakeTime=@FLastWakeTime,");
            strSql.Append("FNextWakeTime=@FNextWakeTime,");
            strSql.Append("FIsAllDayEvent=@FIsAllDayEvent,");
            strSql.Append("FWakeMinutes=@FWakeMinutes,");
            strSql.Append("FInputTime=@FInputTime");
            strSql.Append(" where FCalendarID=@FCalendarID");
            SqlParameter[] parameters = {
					new SqlParameter("@FTitle", SqlDbType.NVarChar,255),
					new SqlParameter("@FContents", SqlDbType.Text),
					new SqlParameter("@FIsWake", SqlDbType.Char,1),
					new SqlParameter("@FBeginDate", SqlDbType.DateTime),
					new SqlParameter("@FEndDate", SqlDbType.DateTime),
					new SqlParameter("@FPri", SqlDbType.Char,1),
					new SqlParameter("@FLastWakeTime", SqlDbType.DateTime),
					new SqlParameter("@FNextWakeTime", SqlDbType.DateTime),
					new SqlParameter("@FIsAllDayEvent", SqlDbType.Char,1),
					new SqlParameter("@FWakeMinutes", SqlDbType.Int,4),
					new SqlParameter("@FInputTime", SqlDbType.DateTime),
					new SqlParameter("@FCalendarID", SqlDbType.Decimal,9),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FEmplNo", SqlDbType.NVarChar,15)};
            parameters[0].Value = model.FTitle;
            parameters[1].Value = model.FContents;
            parameters[2].Value = model.FIsWake;
            parameters[3].Value = model.FBeginDate;
            parameters[4].Value = model.FEndDate;
            parameters[5].Value = model.FPri;
            parameters[6].Value = model.FLastWakeTime;
            parameters[7].Value = model.FNextWakeTime;
            parameters[8].Value = model.FIsAllDayEvent;
            parameters[9].Value = model.FWakeMinutes;
            parameters[10].Value = model.FInputTime;
            parameters[11].Value = model.FCalendarID;
            parameters[12].Value = model.FEntityNo;
            parameters[13].Value = model.FEmplNo;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        //public bool UpdateWakeTime(ElingOA6_DSJCJ.Model.Calendar model)
        //{
        //    StringBuilder strSql = new StringBuilder();
        //    strSql.Append("update dbo.t_Calendar set ");
        //    strSql.Append("FLastWakeTime=@FLastWakeTime,");
        //    strSql.Append("FNextWakeTime=@FNextWakeTime");
        //    strSql.Append(" where FCalendarID=@FCalendarID");
        //    SqlParameter[] parameters = {
        //                                        new SqlParameter("@FLastWakeTime", SqlDbType.DateTime),
        //            new SqlParameter("@FNextWakeTime", SqlDbType.DateTime),
        //                new SqlParameter("@FCalendarID", SqlDbType.Decimal,9)};
        //    parameters[0].Value = DateTime.Now;
        //    parameters[1].Value=model.ReminderInfo
        //    int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        //    if (rows > 0)
        //    {
        //        return true;
        //    }
        //    else
        //    {
        //        return false;
        //    }
        //}
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(decimal FCalendarID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from dbo.t_Calendar ");
            strSql.Append(" where FCalendarID=@FCalendarID");
            SqlParameter[] parameters = {
					new SqlParameter("@FCalendarID", SqlDbType.Decimal)
			};
            parameters[0].Value = FCalendarID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string FCalendarIDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from dbo.t_Calendar ");
            strSql.Append(" where FCalendarID in (" + FCalendarIDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ElingOA6_DSJCJ.Model.Calendar GetModel(decimal FCalendarID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 FCalendarID,FTitle,FContents,FIsWake,FBeginDate,FEndDate,FPri,FLastWakeTime,FNextWakeTime,FIsAllDayEvent,FWakeMinutes,FEntityNo,FEmplNo,FInputTime from dbo.t_Calendar ");
            strSql.Append(" where FCalendarID=@FCalendarID");
            SqlParameter[] parameters = {
					new SqlParameter("@FCalendarID", SqlDbType.Decimal)
			};
            parameters[0].Value = FCalendarID;

            ElingOA6_DSJCJ.Model.Calendar model = new ElingOA6_DSJCJ.Model.Calendar();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ElingOA6_DSJCJ.Model.Calendar DataRowToModel(DataRow row)
        {
            ElingOA6_DSJCJ.Model.Calendar model = new ElingOA6_DSJCJ.Model.Calendar();
            if (row != null)
            {
                if (row["FCalendarID"] != null && row["FCalendarID"].ToString() != "")
                {
                    model.FCalendarID = decimal.Parse(row["FCalendarID"].ToString());
                }
                if (row["FTitle"] != null)
                {
                    model.FTitle = row["FTitle"].ToString();
                }
                if (row["FContents"] != null)
                {
                    model.FContents = row["FContents"].ToString();
                }
                if (row["FIsWake"] != null)
                {
                    model.FIsWake = row["FIsWake"].ToString();
                }
                if (row["FBeginDate"] != null && row["FBeginDate"].ToString() != "")
                {
                    model.FBeginDate = DateTime.Parse(row["FBeginDate"].ToString());
                }
                if (row["FEndDate"] != null && row["FEndDate"].ToString() != "")
                {
                    model.FEndDate = DateTime.Parse(row["FEndDate"].ToString());
                }
                if (row["FPri"] != null)
                {
                    model.FPri = row["FPri"].ToString();
                }
                if (row["FLastWakeTime"] != null && row["FLastWakeTime"].ToString() != "")
                {
                    model.FLastWakeTime = DateTime.Parse(row["FLastWakeTime"].ToString());
                }
                if (row["FNextWakeTime"] != null && row["FNextWakeTime"].ToString() != "")
                {
                    model.FNextWakeTime = DateTime.Parse(row["FNextWakeTime"].ToString());
                }
                if (row["FIsAllDayEvent"] != null)
                {
                    model.FIsAllDayEvent = row["FIsAllDayEvent"].ToString();
                }
                if (row["FWakeMinutes"] != null && row["FWakeMinutes"].ToString() != "")
                {
                    model.FWakeMinutes = int.Parse(row["FWakeMinutes"].ToString());
                }
                if (row["FEntityNo"] != null)
                {
                    model.FEntityNo = row["FEntityNo"].ToString();
                }
                if (row["FEmplNo"] != null)
                {
                    model.FEmplNo = row["FEmplNo"].ToString();
                }
                if (row["FInputTime"] != null && row["FInputTime"].ToString() != "")
                {
                    model.FInputTime = DateTime.Parse(row["FInputTime"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select FCalendarID,FTitle,FContents,FIsWake,FBeginDate,FEndDate,FPri,FLastWakeTime,FNextWakeTime,FIsAllDayEvent,FWakeMinutes,FEntityNo,FEmplNo,FInputTime ");
            strSql.Append(" FROM dbo.t_Calendar ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" FCalendarID,FTitle,FContents,FIsWake,FBeginDate,FEndDate,FPri,FLastWakeTime,FNextWakeTime,FIsAllDayEvent,FWakeMinutes,FEntityNo,FEmplNo,FInputTime ");
            strSql.Append(" FROM dbo.t_Calendar ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM dbo.t_Calendar ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.FCalendarID desc");
            }
            strSql.Append(")AS Row, T.*  from dbo.t_Calendar T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "dbo.t_Calendar";
            parameters[1].Value = "FCalendarID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}

